home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / Bits o' MacApp Code / Windows Menu / WindowsMenu.cp next >
Encoding:
Text File  |  1993-06-10  |  6.5 KB  |  220 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // TWindowMenuApp::DoMenuCommand: 
  3. //----------------------------------------------------------------------------------------
  4. #pragma segment ASelCommand
  5. pascal void TWindowMenuApp::DoMenuCommand(CommandNumber aCommandNumber) // Override 
  6. {
  7.     short    menu, item;
  8.     CommandToComponents(aCommandNumber, menu,item);
  9.     if(aCommandNumber < 0)        // negative items are our window names!
  10.         DoWindowsMenuItem(item);
  11.         
  12.     else {
  13.         switch (aCommandNumber) 
  14.         {
  15.             // Your menu items go here
  16.             
  17.             case cCascade:
  18.                 this->CascadeWindows();
  19.                 break;
  20.                 
  21.             case cCloseAll:
  22.                 this->CloseAllWindows();
  23.                 break;
  24.                 
  25.             default:
  26.                 inherited::DoMenuCommand(aCommandNumber);
  27.                 break;
  28.         }
  29.     }
  30. }
  31.  
  32. //----------------------------------------------------------------------------------------
  33. // TWindowMenuApp::DoSetupMenus: 
  34. //----------------------------------------------------------------------------------------
  35. #pragma segment ARes
  36. pascal void TWindowMenuApp::DoSetupMenus() 
  37. {
  38.     inherited::DoSetupMenus();
  39.     
  40.     // Stuff to enable your menu items goes here
  41.     
  42.     Enable(cCloseAll, true);
  43.     Enable(cCascade, true);
  44.  
  45.     BuildWindowMenu();                    
  46.         
  47.     MenuHandle    windowsMenu = MAGetMenu(mWindows);
  48.     short itemCount = CountMItems(windowsMenu);
  49.     
  50.     // ASSumption Alert - hard coded number of standard items in menu = 3
  51.     //    Close All
  52.     //     Cascade
  53.     //    --------    (Gray dashed line)
  54.     
  55.     for(short item=itemCount; item>3; item--) {
  56.         EnableItem(windowsMenu,item);
  57.     }
  58. }
  59. //----------------------------------------------------------------------------------------
  60. // TWindowMenuApp::BuildWindowMenu: 
  61. //----------------------------------------------------------------------------------------
  62. #pragma segment MAMenuRes
  63. pascal void TWindowMenuApp::BuildWindowMenu()
  64. {
  65.     CStr255    itsTitle;
  66.     MenuHandle    windowsMenu = MAGetMenu(mWindows);
  67.     
  68.     // ASSumption Alert - hard coded number of standard items in menu = 3
  69.     //    Close All
  70.     //     Cascade
  71.     //    --------    (Gray dashed line)
  72.     
  73.     
  74.     short itemCount = CountMItems(windowsMenu);
  75.     for(short item=itemCount; item>3; item--) {
  76.         DelMenuItem(windowsMenu,item);
  77.     }
  78.     
  79.     TList *listOfWindows = MakeWindowList();
  80.     for(ArrayIndex i = 1; i<=listOfWindows->GetSize(); i++) {
  81.         TWindow *aWindow = (TWindow *)listOfWindows->At(i);
  82.         
  83.         // Here we're being *REAL* careful. The thing must be:
  84.         //    1) an Object
  85.         //    2) a TWindow
  86.         //    3) Visible
  87.         // The reason for all this is that MacApp maintains at least 1 offscreen
  88.         // window that we need to ignore (the clipboard window).
  89.         
  90.         if(IsObject(aWindow) && 
  91.            aWindow->IsMemberClass(GetClassIDFromName("TWindow")) &&
  92.            aWindow->IsShown()) {
  93.             
  94.             aWindow->GetTitle(itsTitle);
  95.                AppendMenu(windowsMenu,itsTitle); 
  96.         }
  97.     }
  98.     listOfWindows->Free();        // don't free the TWindows now!
  99. }
  100. //----------------------------------------------------------------------------------------
  101. // TWindowMenuApp::MakeWindowList: 
  102. //----------------------------------------------------------------------------------------
  103. #pragma segment MAWindowRes
  104. pascal TList * TWindowMenuApp::MakeWindowList()
  105. {
  106.  
  107.     WindowPtr *        it = (WindowPtr *)WindowList;
  108.     WindowPeek    aWindowRec = (WindowPeek)*it;    // NOTE: Low mem. global
  109.     WindowPtr    aWindowPtr = *it;
  110.     
  111.     TList *listOfWindows = NewList();
  112.     
  113.     // This works because we know that MacApp places a reference to the 
  114.     // TWindow that "owns" each "WMgrWindow" in the Window Manager's 
  115.     // window list.
  116.     
  117.     do
  118.     {
  119.         listOfWindows->InsertLast((TWindow *)GetWRefCon(aWindowPtr));
  120.         aWindowRec = aWindowRec->nextWindow;
  121.         aWindowPtr = (WindowPtr)aWindowRec;
  122.     }    while (aWindowRec->nextWindow);
  123.     
  124.     return listOfWindows;
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // TWindowMenuApp::CascadeWindows: 
  129. //----------------------------------------------------------------------------------------
  130. #pragma segment MAWindowNonRes
  131. pascal void TWindowMenuApp::CascadeWindows()
  132. {
  133.     TList *listOfWindows = MakeWindowList();
  134.     
  135.     // Now we traverse the list backward, adjusting window locations as we go
  136.  
  137.     CRect    bounds = gStandardWindowMoveBounds;
  138.     bounds.top+=GetMBarHeight();
  139.     for(ArrayIndex i = listOfWindows->GetSize();i>0; i--) {
  140.         TWindow *aWindow = (TWindow *)listOfWindows->At(i);
  141.         
  142.         // Here we're being *REAL* careful. The thing must be:
  143.         //    1) an Object
  144.         //    2) a TWindow
  145.         //    3) Visible
  146.         // The reason for all this is that MacApp maintains at least 1 offscreen
  147.         // window that we need to leave alone (the clipboard window).
  148.         
  149.         if(IsObject(aWindow) && 
  150.            aWindow->IsMemberClass(GetClassIDFromName("TWindow")) &&
  151.            aWindow->IsShown()) {
  152.            
  153.            // Why not use TWindow::SimpleStagger? Because I couldn't get it to
  154.            // work. Somebody should figure out why before we go live!    WLC
  155.            
  156.             aWindow->Select();
  157.             aWindow->Locate(bounds[topLeft], kDontInvalidate);
  158.             bounds.top+=kStdStaggerAmount;
  159.             bounds.left+=kStdStaggerAmount;
  160.         }
  161.     }
  162.     listOfWindows->Free();        // don't free the TWindows now!
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // TWindowMenuApp::CloseAllWindows: 
  167. //----------------------------------------------------------------------------------------
  168. #pragma segment MAWindowNonRes
  169. pascal void TWindowMenuApp::CloseAllWindows()
  170. {
  171.     
  172.     TList *listOfWindows = MakeWindowList();
  173.     
  174.     // Now we traverse the list, closing as we go (front to back)
  175.     
  176.     for(ArrayIndex i = 1;i<=listOfWindows->GetSize(); i++) {
  177.         TWindow *aWindow = (TWindow *)listOfWindows->At(i);
  178.         
  179.         // Here we're being *REAL* careful. The thing must be:
  180.         //    1) an Object
  181.         //    2) a TWindow
  182.         //    3) Visible
  183.         // The reason for all this is that MacApp maintains at least 1 offscreen
  184.         // window that we need to leave alone (the clipboard window).
  185.         
  186.         if(IsObject(aWindow) && 
  187.            aWindow->IsMemberClass(GetClassIDFromName("TWindow")) &&
  188.            aWindow->IsShown()) {
  189.             aWindow->CloseByUser();
  190.         }
  191.     }
  192.     listOfWindows->Free();        // don't free the TWindows now!
  193. }
  194. #pragma segment ASelCommand
  195. pascal void TWindowMenuApp::DoWindowsMenuItem(short item)
  196. {
  197.     CStr255    windowTitle, itsTitle;
  198.     MenuHandle    windowsMenu = MAGetMenu(mWindows);
  199.     
  200.     GetItem(windowsMenu, item, windowTitle);
  201.     
  202.     TList *listOfWindows = MakeWindowList();
  203.     for(ArrayIndex i = 1; i<=listOfWindows->GetSize(); i++) {
  204.         TWindow *aWindow = (TWindow *)listOfWindows->At(i);
  205.         
  206.         if(IsObject(aWindow) && 
  207.            aWindow->IsMemberClass(GetClassIDFromName("TWindow")) &&
  208.            aWindow->IsShown()) {
  209.             
  210.             aWindow->GetTitle(itsTitle);
  211.             if(itsTitle == windowTitle) {
  212.                 aWindow->Select();
  213.                 break;
  214.             }
  215.         }
  216.     }
  217.     listOfWindows->Free();        // don't free the TWindows now!
  218. }
  219.  
  220.